/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sfs2x.extension.fourinarow.main;

import com.smartfoxserver.bitswarm.sessions.ISession;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSConstants;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSLoginException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author dodinhchien
 */
public class LoginEventHandle extends BaseServerEventHandler {

    @Override
    public void handleServerEvent(ISFSEvent event) throws SFSException {
        FourinarowExtension extension = (FourinarowExtension) getParentExtension();
        //String username = (String) event.getParameter(SFSEventParam.LOGIN_NAME);
        //String password = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD);
        ISession session = (ISession)event.getParameter(SFSEventParam.SESSION);
        
        ISFSObject indata = (SFSObject) event.getParameter(SFSEventParam.LOGIN_IN_DATA);
        if(!indata.containsKey("mail") || !indata.containsKey("ref") || !indata.containsKey("os") || !indata.containsKey("device")){
            throw new SFSLoginException("input data error", new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME));
        }
        User user = getParentExtension().getParentZone().getUserByName(indata.getUtfString("device"));
        if(user != null){
            getApi().disconnectUser(user);
            /*SFSErrorData dt = new SFSErrorData(SFSErrorCode.LOGIN_ALREADY_LOGGED);
            dt.addParameter(indata.getUtfString("device"));
            throw  new SFSLoginException("already login " + indata.getUtfString("device"), dt);*/
        }
        try {
            Connection conn; 
            conn = getParentExtension().getParentZone().getDBManager().getConnection();
            PreparedStatement sql = conn.prepareStatement("{call RegistUser(?,?,?,?)}");
            sql.setString(1, indata.getUtfString("mail"));
            sql.setString(2, indata.getUtfString("ref"));
            sql.setInt(3, indata.getInt("os"));
            sql.setString(4, indata.getUtfString("device"));
            ResultSet result = sql.executeQuery();
            SFSArray row = SFSArray.newFromResultSet(result);
            ISFSObject outData = (ISFSObject) event.getParameter(SFSEventParam.LOGIN_OUT_DATA);
            outData.putUtfString(SFSConstants.NEW_LOGIN_NAME, indata.getUtfString("device"));
            outData.putInt("mark", row.getSFSObject(0).getInt("mark"));
            
            trace(extension.getArrayMC());
            outData.putSFSArray("arrm", extension.getArrayMC());
            conn.close();
            session.setProperty("mark", row.getSFSObject(0).getInt("mark"));
            
        } catch (SQLException ex) {
            trace(ExtensionLogLevel.WARN, " SQL Failed: " + ex.toString());
        }
    }

}
